﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace VisualStudioAchievements
{
    public static class Util
    {
        public static Achievement NotEarnedAchievement = new Achievement() { FriendlyName = "Not Earned", Icon = new Uri("/Images/locked.png", UriKind.Relative) };

        public static void SerializeNiners()
        {
            var userStore = IsolatedStorageFile.GetUserStoreForApplication();

            using (var stream = new IsolatedStorageFileStream("niners.xml", FileMode.Create, userStore))
            {
                XmlSerializer serializer = new XmlSerializer(BindingPoint.Instance.Niners.GetType());
                serializer.Serialize(stream, BindingPoint.Instance.Niners);
            }
        }

        public static void DeserializeNiners()
        {
            BindingPoint.Instance.Niners = new ObservableCollection<Niner>();

            var userStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (userStore.FileExists("niners.xml"))
            {
                using (var stream = new IsolatedStorageFileStream("niners.xml", FileMode.Open, userStore))
                {
                    XmlSerializer serializer = new XmlSerializer(BindingPoint.Instance.Niners.GetType());
                    var niners = (ObservableCollection<Niner>)serializer.Deserialize(stream);

                    foreach (Niner niner in niners)
                    {
                        NinerReader reader = new NinerReader();
                        reader.GetNiner(niner.Alias, true, extractedNiner =>
                        {
                            BindingPoint.Instance.Niners.Add(extractedNiner);
                        });
                    }
                }
            }
        }

        public static void ListComparedAchievements()
        {
            BindingPoint.Instance.ComparedAchievements = new ObservableCollection<CompareAchievementPair>();
            // There is going to be at least one achievement for a single user - the one 
            // assigned on registration. Currently there is no need to check whether the achievement
            // list is empty.
            foreach (var achievement in BindingPoint.Instance.ComparedNiners.FirstNiner.Achievements)
            {
                var pair = new CompareAchievementPair();
                pair.FirstAchievement = achievement;

                try
                {
                    // Comparison is done through FriendlyName and not by the instance because of different earned dates.
                    var secondAchievement = (from c in BindingPoint.Instance.ComparedNiners.SecondNiner.Achievements where c.FriendlyName == achievement.FriendlyName select c).Single();
                    pair.SecondAchievement = secondAchievement;
                }
                catch
                {
                    pair.SecondAchievement = NotEarnedAchievement;
                }

                BindingPoint.Instance.ComparedAchievements.Add(pair);
            }

            foreach (var achievement in BindingPoint.Instance.ComparedNiners.SecondNiner.Achievements)
            {
                try
                {
                    var selectedAchievement = (from c in BindingPoint.Instance.ComparedAchievements where c.SecondAchievement.FriendlyName == achievement.FriendlyName select c).Single();
                }
                catch
                {
                    var pair = new CompareAchievementPair();
                    pair.FirstAchievement = NotEarnedAchievement;
                    pair.SecondAchievement = achievement;
                    BindingPoint.Instance.ComparedAchievements.Add(pair);
                }
            }

            BindingPoint.Instance.HidePerfBar = true;
        }

        public static bool CheckIfUserExists(string name)
        {
            Niner selected = (from c in BindingPoint.Instance.Niners where c.Alias == name select c).FirstOrDefault();
            if (selected != null)
                return true;
            else
                return false;
        }

        public static DependencyObject FindChildInTree(DependencyObject parent, int depth)
        {
            DependencyObject lookupObject = null;
            for (int i = 0; i < depth; i++)
            {
                if (lookupObject == null)
                    lookupObject = VisualTreeHelper.GetChild(parent, 0);
                else
                    lookupObject = VisualTreeHelper.GetChild(lookupObject, 0);
            }
            return lookupObject;
        }
    }
}
